<?php
class H_BaseCate extends H_Base{
	//------------------------------------------------
         public function add($data){
            $this->urlProcess($data);
            $this->filterData($data,$cat);
            $lastId = $this->insert($data);
            return $lastId;
        }
        public function edit($id,$data){
            $this->urlProcess($data);
            $this->filterData($data,$cat);
            $where = $this->_db->quoteInto("id = ?", $id);
            return $this->update($data, $where);
        }
	public function getCateTree($lang,$pid = 0,$dept = 1){
		$select = $this->_db->select();
		$select->from($this->_name,array('id','title','pid','order'))
			   ->where('pid = ?',$pid)
			   ->where('lang =?',$lang)
			   ->order("order ASC")
			   ->order("title ASC");
		$result = $this->_db->fetchAll($select, Zend_Db::FETCH_ASSOC);
		if (count($result)>0){
			foreach ($result as $key => $item){
				$result[$key]['dept'] = $dept;
				$result[$key]['child'] = $this->getCateTree($lang,$item['id'],$dept+1); 
			}
		}else{
			return null;
		}
		return $result;   
	}	
	
	public function delCate($id){
		$tmp = $this->find($id)->toArray();
		$pID = $tmp[0]['pid'];
		$select = $this->_db->select();
		$select->from($this->_name,array('id'))
			   ->where("pid = ?",$id);		
		$child = $this->_db->fetchAll($select, Zend_Db::FETCH_ASSOC);
		
		foreach ($child as $item){
			$dataUpdate = array('pid' => $pID);
			$this->edit($item['id'], $dataUpdate);
		}	
		$where = $this->_db->quoteInto("id = ?", $id);
		return $this->delete($where);
	}

        public function getParentId($catId){
            $select = $this->_db->select();
            $select->from($this->_name,array('pid'))
                    ->where("id = ?",$catId);
            return $this->_db->fetchOne($select);
        }
}